﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace VideoScreensaver
{
    public partial class ConfigWindow : Form
    {
        public ConfigWindow()
        {
            Application.EnableVisualStyles();
            InitializeComponent();

            // read in saved configuration details
            List<String> videos = PreferenceManager.ReadVideoSettings();
            double volume = PreferenceManager.ReadVolumeSetting();
            bool shuffle = PreferenceManager.ReadShuffleSetting();
            bool blacken_monitors = PreferenceManager.ReadBlackenSetting();

            // set the controls based on this information
            // videos list
            if (videos.Count > 0)
            {
                ListBoxVideo.Items.AddRange(videos.ToArray());
                ListBoxVideo.SelectedIndex = 0;
            }
            else
            {
                ListBoxVideo.SelectedIndex = -1;
            }

            // shuffle button
            CheckBoxShuffle.Checked = shuffle;

            // volume box
            VolumeBox.Value = Convert.ToDecimal((volume * 100.0));
            if (VolumeBox.Value == 0)
                CheckBoxMute.Checked = true;
            else
                CheckBoxMute.Checked = false;

            // blacken secondary monitors control
            CheckBoxBlacken.Checked = blacken_monitors;

        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            // save settings
            // video list
            List<String> videos = new List<String>();
            foreach (String s in ListBoxVideo.Items)
                videos.Add(s);
            PreferenceManager.WriteVideoSettings(videos);

            // shuffle video button
            PreferenceManager.WriteShuffleSetting(CheckBoxShuffle.Checked);

            // default volume
            if (CheckBoxMute.Checked)
                PreferenceManager.WriteVolumeSetting(0);
            else
                PreferenceManager.WriteVolumeSetting((double)(Convert.ToDouble(VolumeBox.Value) / 100.0));

            // blacken secondary monitors
            PreferenceManager.WriteBlackenSetting(CheckBoxBlacken.Checked);

            // close this window
            this.Close();
        }

        private void CheckBoxMute_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBoxMute.Checked)
                VolumeBox.Enabled = false;
            else
                VolumeBox.Enabled = true;
        }

        private void ListBoxVideo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListBoxVideo.Items.Count > 0)
            {
                ButtonClear.Enabled = true;
                ButtonRemove.Enabled = true;
            }
            else
            {
                ButtonClear.Enabled = false;
                ButtonRemove.Enabled = false;
            }
        }

        private void ButtonClear_Click(object sender, EventArgs e)
        {
            ListBoxVideo.Items.Clear();
        }

        private void ButtonRemove_Click(object sender, EventArgs e)
        {
            int i = ListBoxVideo.SelectedIndex;
            ListBoxVideo.Items.RemoveAt(i);
            if (i >= ListBoxVideo.Items.Count)
            {
                i = ListBoxVideo.Items.Count - 1;
            }
            ListBoxVideo.SelectedIndex = i;
        }

        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Multiselect = true;
            openDialog.Title = "Select video files to display...";
            openDialog.Filter = "Video Files(*.avi;*.mkv;*.mp4;*.wmv;*.divx)|*.avi;*.mkv;*.mp4;*.wmv;*.divx|All files (*.*)|*.*";
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                List<String> videos = new List<String>();
                videos.AddRange(openDialog.FileNames);

                if (videos.Count > 0)
                {
                    ListBoxVideo.Items.AddRange(videos.ToArray());
                    ListBoxVideo.SelectedIndex = 0;
                }
                else
                {
                    ListBoxVideo.SelectedIndex = -1;
                }

            }
        }

    }
}
